package com.sinosoft.tk.mybatis.mapper.uitl;

import tk.mybatis.mapper.entity.EntityColumn;
import tk.mybatis.mapper.util.StringUtil;

/**
 * @Auther: zouren
 * @Date: 2019/3/29 10:04
 * @Description:
 */
public class MySqlHelper {
    public static String getIfNotNull(EntityColumn column, String contents, boolean empty) {
        return getIfNotNull((String)null, column, contents, empty);
    }
    public static String getIfNotNull(String entityName, EntityColumn column, String contents, boolean empty) {
        StringBuilder sql = new StringBuilder();
        sql.append("<if test=\"");
        if (StringUtil.isNotEmpty(entityName)) {
            sql.append(entityName).append(".");
        }

        sql.append(column.getProperty()).append(" != null");
        if (empty && column.getJavaType().equals(String.class)) {
            sql.append(" and ");
            if (StringUtil.isNotEmpty(entityName)) {
                sql.append(entityName).append(".");
            }

            sql.append(column.getProperty()).append(" != '' ");
            sql.append("\">");
            sql.append(contents);

        }else{
            sql.append("\">");
            sql.append("null");
        }
        sql.append("</if>");

        return sql.toString();
    }
}
